package com.example.gzysmartcity.ui.fragment.home;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.blankj.utilcode.util.ToastUtils;
import com.example.gzysmartcity.R;
import com.example.gzysmartcity.databinding.FragmentNewsxqBinding;
import com.example.gzysmartcity.repository.model.News;
import com.example.gzysmartcity.repository.model.SCNewsComment;
import com.example.gzysmartcity.ui.adapter.NewsXpRvAdapter;
import com.example.gzysmartcity.ui.base.BaseFragment;
import com.example.gzysmartcity.viewmodel.NewsXpViewModel;

import org.jetbrains.annotations.NotNull;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

public class NewsXqFragment extends BaseFragment<FragmentNewsxqBinding> {

    @Override
    protected FragmentNewsxqBinding viewBinding() {
        return FragmentNewsxqBinding.inflate(getLayoutInflater(), baseBinding.getRoot(), true);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onViewCreated(@NonNull @NotNull View view, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Bundle arguments = getArguments();
        if (arguments == null) return;
        if (arguments.getBoolean("isSearch", false)) {
        showBottom=false;
        }
        News news = (News) arguments.getSerializable("html");
        Log.i("TAG", "onViewCreated: " + arguments.getSerializable("html"));
        b.webViewNewsxp.loadData(news.getHtml(), "text/html", "utf-8");
        b.tvNewsxpZuozhe.setText("作者: " + news.getAuthor());
        b.tvNewsxpTitle.setText(news.getContent());
        SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd kk:mm", Locale.CHINA);
        b.tvNewsxpTime.setText(mSimpleDateFormat.format(new Date((long) news.getTime() * 1000)));
        Log.i("TAG", "onViewCreated: "+news.getTime());
        NewsXpViewModel newsXpViewModel = new ViewModelProvider(this).get(NewsXpViewModel.class);
        newsXpViewModel.getNewsCommentData();
        NewsXpRvAdapter adapter = new NewsXpRvAdapter(R.layout.rv_newsxq);
        newsXpViewModel.getNewsXpLiveData().observe(getViewLifecycleOwner(), state -> {
            switch (state) {
                case SUCCESS:
                    List<SCNewsComment.DataBean> dataBeanList = new ArrayList<>(newsXpViewModel.getNewIdList(news.getId()));
                    adapter.setNewInstance(dataBeanList);
                    b.rvNewsxp.setAdapter(adapter);
                    b.rvNewsxp.setLayoutManager(new LinearLayoutManager(getContext()));
                    break;
                case FAIL:
                    ToastUtils.showShort("评论请求失败");
                    break;
            }
        });

    }

    @Override
    protected String setTitle() {
        return "新闻详情";
    }

    @Override
    protected boolean showBack() {
        return true;
    }

    @Override
    protected Boolean showBottom() {
        return null;
    }
}
